<?php

class DesignerController extends GTL_Action {

    public function init() {
        parent::init();
        /* Initialize action controller here */
    }

    public function indexAction() {
        $this->_helper->redirector('list', 'designer', '');
    }

    public function listAction() {

        $errorMessage = Array();
        $successmessage = "";
        $where = "";
        $searchtype = "";
        $searchtext = "";
        $searchstatus = "";
        $datefrom = '';
        $dateto = '';
        $orderField = 'dsg_id';
        $sort = 'asc';

        /* Pagination Login */
        $itemsPerPageReviews = $this->config->PAGING_PER_PAGE;
        $currentPageReviews = 1;

        //PAGING_PER_PAGE

        if ($this->_getParam('page') && $this->_getParam('page') != "") {
            $currentPageReviews = $this->_getParam('page');
        }

        if ($this->_getParam('field') && $this->_getParam('field') != "") {
            $orderField = $this->_getParam('field');
        }

        if ($this->_getParam('sort') && $this->_getParam('sort') != "") {
            $sort = $this->_getParam('sort');
        }


        $offset = ($currentPageReviews - 1) * $itemsPerPageReviews;

        if ($this->_request->isPost()) {
            $postedData = $this->_request->getPost();

            //Multiple Delete Logic 
            if (isset($postedData["multiaction"]) && ($postedData["multiaction"] != "")) {
                $action = trim($postedData["multiaction"]);
                if (isset($postedData["multicheck"]) && count($postedData["multicheck"]) > 0) {
                    $result = $this->deleteAction($_POST["multicheck"]);
                } else {
                    $errorMessage[] = "Please select atleast one checkbox";
                }
            }

            /* Search Facility Over Here */
            if (isset($postedData["searchtype"]) && ($postedData["searchtype"] != "")) {
                $searchtype = $postedData["searchtype"];
                if ($searchtype == "dsg_name" ||
                        $searchtype == "lgn_email" ||
                        $searchtype == "dsg_designation" ||
                        $searchtype == "dsg_telephone" ||
                        $searchtype == "dsg_mobile") {
                    $searchtext = $postedData["searchtext"];
                    if ($searchtext == '') {
                        $errorMessage[] = "Please enter search text for search.";
                    } else {
                        $where = "$searchtype LIKE '%$searchtext%'";
                    }
                } else if ($searchtype == "lgn_active") {
                    $searchstatus = $postedData["searchstatus"];
                    if ($searchstatus == '') {
                        $errorMessage[] = "Please select status type for search.";
                    } else {
                        $where = "`lgn_active` = '" . $searchstatus . "' ";
                    }
                } else if ($searchtype == 'daterange') {
                    $booldate = false;
                    $datefrom = $postedData['datefrom'];
                    $dateto = $postedData['dateto'];

                    if (GTL_Common::IsEmpty($datefrom)) {
                        $errorMessage[] = 'Please enter from date.';
                    } else if (!GTL_Common::IsValidDate($datefrom)) {
                        $errorMessage[] = 'Please enter proper from date in YYYY-MM-DD.';
                    } else {
                        $booldate = true;
                    }

                    if (GTL_Common::IsEmpty($dateto)) {
                        $errorMessage[] = 'Please enter to date.';
                        $booldate = false;
                    } else if (!GTL_Common::IsValidDate($dateto)) {
                        $errorMessage[] = 'Please enter proper to date in YYYY-MM-DD.';
                        $booldate = false;
                    } else {
                        $booldate = true;
                    }

                    if ($booldate) {
                        $where = "dsg_joineddate BETWEEN  '" . $datefrom . "' AND '" . $dateto . "  23:59:59" . "' ";
                    }
                }
            }
        }

        $designerModel = new Model_Designer();
        $designersData = $designerModel->fetchDesigners($where, $itemsPerPageReviews, $offset, $orderField, $sort);

        $paginator = new GTL_Paginator();
        $paginator->setItemsPerPage($itemsPerPageReviews);
        $paginator->setItemsTotal($designerModel->getDesignerCount($where));
        $paginator->setCurrentPage($currentPageReviews);
        $link = $this->view->url(array('controller' => 'designer', 'action' => 'list', 'page' => 'PAGENO'), 'default', false);
        $paginator->paginate($link);

        $this->view->paginator = $paginator;
        $this->view->designersData = $designersData;

        $this->view->errorMessage = $errorMessage;
        $this->view->successmessage = $successmessage;
        $this->view->statusArray = $designerModel->getStatusArray();

        $this->view->searchtype = $searchtype;
        $this->view->searchtext = $searchtext;
        $this->view->searchstatus = $searchstatus;
        $this->view->datefrom = $datefrom;
        $this->view->dateto = $dateto;

        $indexUrl = $this->view->url(array('controller' => 'index', 'action' => 'index'), null, TRUE);
        $designerUrl = $this->view->url(array('controller' => 'designer', 'action' => 'list'), null, TRUE);
        $this->view->breadcrumbs = array($indexUrl => "Home", $designerUrl => "Designer", "" => "Designer Listing");
    }

    public function deleteAction($ids = Array()) {
        //Load Designer Model 
        $designerModel = new Model_Designer();
        if ($this->_getParam('dsg_id') && $this->_getParam('dsg_id') != "") {
            $dsg_id = $this->_getParam('dsg_id');
            $designerModel->deleteDesigner($dsg_id);
            $this->addSuccessMessage("Designer deleted succesfully");
            $this->_helper->redirector('list', 'designer', '');
        } else {
            $designerModel->deleteDesigner($ids);
            $this->addSuccessMessage("Designer deleted succesfully");
            $this->_helper->redirector('list', 'designer', '');
        }
    }

    public function formAction() {

        //Initialize
        $successmessage = "";
        $errorMessageArray = Array();
        $data = array();
        $designerData = Array();
        $loginData = Array();
        $password = "";
        $currentEmail = "";
        $this->view->mode = "Add";

        //Load Designer Model
        $designerModel = new Model_Designer();

        //Load login Model
        $loginModel = new Model_Login();

        //Load Designer Form
        $designerForm = new Form_DesignerForm();

        //Load the status array for the select box in form 
        $statusArray = $designerModel->getStatusArray();
        $designerForm->lgn_active->addMultiOptions($statusArray);

        //Check for the designer Parameter
        $designerData['dsg_id'] = $this->_request->getParam('dsg_id');
        $dsg_id = (!empty($designerData['dsg_id'])) ? $designerData['dsg_id'] : null;

        if (!empty($dsg_id)) {
            //Set the Mode to Edit
            $this->view->mode = "Edit";
            //Fetch data to be loaded in form 
            $data = $designerModel->fetchEntryById($dsg_id);
            $lgn_id = $data['lgn_id'];
            $currentEmail = $data['lgn_email'];
        }

        //Populate form 
        $this->view->form = $designerForm->populate($data);

        if ($this->getRequest()->isPost()) {

            //Check for Errors
            $postedData = $this->_request->getPost();
            if (!$designerForm->isValid($postedData)) {
                $errorMessage = $designerForm->getMessages();
                foreach ($errorMessage as $_err) {
                    foreach ($_err as $_val) {
                        $errorMessageArray[] = $_val;
                    }
                }
            } else {
                if ($this->view->mode == "Add") {
                    //Check for the email exist while Adding custoemr
                    $designerExist = $loginModel->checkEmailExist($postedData['lgn_email']);
                    if ($designerExist) {
                        $errorMessageArray[] = "Email Adress already exists!";
                    }
                } else {
                    //Check for the email exist while Editing designer
                    $designerExist = $loginModel->checkEmail($currentEmail, $postedData['lgn_email']);
                    if (!$designerExist) {
                        $errorMessageArray[] = "Email Adress already exists!";
                    }
                }

                //Password Validation
                if ($postedData['dsg_password'] != "") {
                    if ($postedData['dsg_confirmpassword'] == "") {
                        $errorMessageArray[] = "Confirm Password should not be blank.";
                    }
                    if ($postedData['dsg_password'] != $postedData['dsg_confirmpassword']) {
                        $errorMessageArray[] = "Password and Confirm Password should not be blank.";
                    }
                }

                if (count($errorMessageArray) > 0) {
                    
                } else {

                    //If mode is Add, create a new login ID 
                    if ($this->view->mode == "Add") {
                        $password = md5($postedData['dsg_password']);
                        $loginData['lgn_email'] = $postedData['lgn_email'];
                        $loginData['lgn_password'] = $password;
                        $loginData['lgn_type'] = '4';
                        $loginData['lgn_created'] = date("Y-m-d H:i:s");
                        $loginData['lgn_modified'] = date("Y-m-d H:i:s");
                        $loginData['lgn_active'] = $postedData['lgn_active'];
                        $lgn_id = $loginModel->save($loginData, 'lgn_id');
                    } else {
                        if ($postedData['dsg_password'] != "") {
                            $password = ($postedData['dsg_password'] == "") ? "" : md5($postedData['dsg_password']);
                            $loginData['lgn_email'] = $postedData['lgn_email'];
                            $loginData['lgn_id'] = $lgn_id;
                            $loginData['lgn_password'] = $password;
                            $loginData['lgn_modified'] = date("Y-m-d H:i:s");
                            $loginData['lgn_active'] = $postedData['lgn_active'];
                            $lgn_id = $loginModel->save($loginData, 'lgn_id');
                        } else {
                            $loginData['lgn_id'] = $lgn_id;
                            $loginData['lgn_active'] = $postedData['lgn_active'];
                            $lgn_id = $loginModel->save($loginData, 'lgn_id');
                        }
                    }

                    //Designer save
                    $designerData['dsg_id'] = $dsg_id;
                    $designerData['lgn_id'] = $lgn_id;
                    $designerData['dsg_name'] = $postedData['dsg_name'];
                    $designerData['dsg_joineddate'] = $postedData['dsg_joineddate'];
                    $designerData['dsg_designation'] = $postedData['dsg_designation'];
                    $designerData['dsg_address'] = $postedData['dsg_address'];
                    $designerData['dsg_telephone'] = $postedData['dsg_telephone'];
                    $designerData['dsg_mobile'] = $postedData['dsg_mobile'];
                    $designerModel->save($designerData, 'dsg_id');
                    if ($this->view->mode == "Add") {
                        $successmessage = "Designer added successfully";
                    } else {
                        $successmessage = "Designer updated successfully";
                    }
                    $this->addSuccessMessage($successmessage);
                    $this->_helper->redirector('list', 'designer', '');
                }
            }
        }

        $this->view->errorMessageArray = $errorMessageArray;
        $this->view->succesMessage = $successmessage;
        $this->view->form = $designerForm;
        $indexUrl = $this->view->url(array('controller' => 'index', 'action' => 'index'), null, TRUE);
        $designerUrl = $this->view->url(array('controller' => 'designer', 'action' => 'list'), null, TRUE);
        $this->view->breadcrumbs = array($indexUrl => "Home", $designerUrl => "Designer", "" => $this->view->mode . " Designer");
    }

    public function profileAction() {

        //Validation that only designer can change his profile
        if ($this->USER->role != "DESIGNER") {
            $this->_helper->redirector('index', 'index', '');
        }

        //Initialize
        $successmessage = "";
        $errorMessageArray = Array();
        $data = array();
        $designerData = Array();
        $loginData = Array();
        $password = "";

        //Load Designer Model
        $designerModel = new Model_Designer();

        //Load login Model
        $loginModel = new Model_Login();

        //Load Designer Form
        $designerForm = new Form_DesignerForm();

        //Load the status array for the select box in form 
        $statusArray = $designerModel->getStatusArray();
        $designerForm->lgn_active->addMultiOptions($statusArray);

        $designerData['dsg_id'] = $this->USER->usr_id;
        $dsg_id = (!empty($designerData['dsg_id'])) ? $designerData['dsg_id'] : null;

        //Fetch data to be loaded in form 
        $data = $designerModel->fetchEntryById($dsg_id);
        $lgn_id = $data['lgn_id'];

        //Populate form 
        $this->view->form = $designerForm->populate($data);

        if ($this->getRequest()->isPost()) {

            //Check for Errors
            $postedData = $this->_request->getPost();
            if (!$designerForm->isValid($postedData)) {
                $errorMessage = $designerForm->getMessages();
                foreach ($errorMessage as $_err) {
                    foreach ($_err as $_val) {
                        $errorMessageArray[] = $_val;
                    }
                }
            } else {

                //Password Validation
                if ($postedData['dsg_password'] != "") {
                    if ($postedData['dsg_confirmpassword'] == "") {
                        $errorMessageArray[] = "Confirm Password should not be blank.";
                    }
                    if ($postedData['dsg_password'] != $postedData['dsg_confirmpassword']) {
                        $errorMessageArray[] = "Password and Confirm Password should not be blank.";
                    }
                }

                if (count($errorMessageArray) > 0) {
                    
                } else {

                    if ($postedData['dsg_password'] != "") {
                        $password = ($postedData['dsg_password'] == "") ? "" : md5($postedData['dsg_password']);
                        $loginData['lgn_email'] = $postedData['lgn_email'];
                        $loginData['lgn_id'] = $lgn_id;
                        $loginData['lgn_password'] = $password;
                        $loginData['lgn_modified'] = date("Y-m-d H:i:s");
                        $loginData['lgn_active'] = $postedData['lgn_active'];
                        $lgn_id = $loginModel->save($loginData, 'lgn_id');
                    }

                    //Designer save
                    $designerData['dsg_id'] = $dsg_id;
                    $designerData['lgn_id'] = $lgn_id;
                    $designerData['dsg_name'] = $postedData['dsg_name'];
                    $designerData['dsg_joineddate'] = $postedData['dsg_joineddate'];
                    $designerData['dsg_designation'] = $postedData['dsg_designation'];
                    $designerData['dsg_address'] = $postedData['dsg_address'];
                    $designerData['dsg_telephone'] = $postedData['dsg_telephone'];
                    $designerData['dsg_mobile'] = $postedData['dsg_mobile'];
                    $designerModel->save($designerData, 'dsg_id');
                    $successmessage = "Profile updated successfully";
                    $this->addSuccessMessage($successmessage);
                }
            }
        }

        $this->view->errorMessageArray = $errorMessageArray;
        $this->view->succesMessage = $successmessage;
        $this->view->form = $designerForm;

        $indexUrl = $this->view->url(array('controller' => 'index', 'action' => 'index'), null, TRUE);
        $profileUrl = $this->view->url(array('controller' => 'designer', 'action' => 'profile'), null, TRUE);
        $this->view->breadcrumbs = array($indexUrl => "Home", "" => "Edit profile");
    }

}

?>